package edu.zut.cs.software.perfect.grade.web.spring.controller;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;

import edu.zut.cs.software.perfect.base.web.spring.controller.GenericController;
import edu.zut.cs.software.perfect.grade.domain.StudentGrade;
import edu.zut.cs.software.perfect.grade.service.StudentGradeManager;

/**
 * @ClassName StudentGradeController
 * @Description TODO
 * @Author zhangwenkai
 * @Date 2020年5月30日 上午12:21:57
 * @Version 1.0
 */
@ResponseBody
@Controller
@RequestMapping("/studentGrade")
public class StudentGradeController extends  GenericController<StudentGrade,Long,StudentGradeManager> {
	
	StudentGradeManager  studentGradeManager;
	/**
	 * The manager is injected into the controller
	 */
	@Autowired
	public void setStudentGradeManager(StudentGradeManager studentGradeManager) {
		this.studentGradeManager = studentGradeManager;
		this.manager = studentGradeManager;
	}
	
	@ResponseBody
    @GetMapping(value = "findall", produces = "application/json;charset=utf-8")
	public String findAllStudentGrade() {
		List<StudentGrade> studentGradeList = this.manager.findAll();
		PageInfo pageInfo = new PageInfo(studentGradeList);
		return JSON.toJSONString(pageInfo);
	}
	
	@GetMapping("/findbyid/{id}")
	public String findById(@PathVariable("id") Long id) {
		Optional<StudentGrade> result = this.manager.findByCourseId(id);
		return JSON.toJSONString(result);
	}
	
	@GetMapping("/save/{name}")
	public StudentGrade save(@PathVariable("name") String studentName) {
		StudentGrade studentGrade = new StudentGrade();
		studentGrade.setStudentName(studentName);
		StudentGrade result = studentGradeManager.save(studentGrade);
		return result;
	}
	
	@GetMapping("/deletebyid/{id}")
	public Boolean deleteById(@PathVariable("id") Long id) {
		Boolean result = this.manager.deleteById(id);
		return result;
	}
	
	@GetMapping("/updata/{id}")
	public StudentGrade updata(@PathVariable("id") Long id) {
		Optional<StudentGrade> studentGrade = this.manager.findByCourseId(id);
		StudentGrade s = studentGrade.get();
		s.setStudentName("ZWK");
		StudentGrade result = this.manager.save(s);
		return result;
	}

}
